package es.us.isa.jdbl.data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;

import es.us.isa.jdbl.domain.Member;
import es.us.isa.jdbl.domain.biblio.Book;
import es.us.isa.jdbl.domain.biblio.Contributors;
import es.us.isa.jdbl.domain.biblio.ContributorsType;
import es.us.isa.jdbl.domain.biblio.Thesis;

public class ThesisDAO {

	public static List<Thesis> getAll(EntityManager em){ 
		
		return  em.createQuery("from Thesis").getResultList();
		
	}
		
	public static List<Thesis> getDirectedThesis(EntityManager em, Member m){
		List<Thesis> lista = em.createQuery("from Thesis").getResultList();
		List<Thesis> tesisMiembro = new LinkedList<Thesis>();
		List<Thesis> resultado = new LinkedList<Thesis>();
		
		for (Thesis aux:lista){
			for (Contributors c:aux.getAuthors()){
				if(c.getMemberProfile() != null){
					
					if(c.getMemberProfile().equals(m)){
						tesisMiembro.add(aux);
					}
				}
			}
		}
		
		for(Thesis t:tesisMiembro){
			List<Member> directores = getDirectors(em, t);
			if (directores.contains(m)){
				resultado.add(t);
			}
		}
		
		return resultado;
		
	}
	
	public static List<Thesis> getParticipantThesis(EntityManager em, Member m){
		List<Thesis> lista = em.createQuery("from Thesis").getResultList();
		List<Thesis> tesisMiembro = new LinkedList<Thesis>();
		List<Thesis> resultado = new LinkedList<Thesis>();
		
		for (Thesis aux:lista){
			for (Contributors c:aux.getAuthors()){
				if(c.getMemberProfile() != null){
					
					if(c.getMemberProfile().equals(m)){
						tesisMiembro.add(aux);
					}
				}
			}
		}
		
		for(Thesis t:tesisMiembro){
			List<Member> miembros = getAuthors(em, t);
			if (miembros.contains(m)){
				resultado.add(t);
			}
		}
		
		return resultado;
		
	}
	
	
	
	
	public static List<Member> getDirectors(EntityManager em, Thesis t){
		List<ContributorsType> contributorsType = em.createQuery("from ContributorsType where nid=" + t.getId() + " and authCategory=3").getResultList();
		List<Member> members = null;
		if(contributorsType.size() != 0){
			members = new ArrayList<Member>();
			for(ContributorsType ct : contributorsType){
				Contributors c = (Contributors) em.createQuery("from Contributors where cid=" + ct.getCid() + "").getSingleResult();
				Member m = c.getMemberProfile();
				if(m != null){
					members.add(m);
				}	
			}
		}
		return members;
	}
	
	public static List<Member> getAuthors(EntityManager em, Thesis t){
		List<ContributorsType> contributorsType = em.createQuery("from ContributorsType where nid=" + t.getId() + " and authCategory=1").getResultList();
		List<Member> members = null;
		if(contributorsType.size() != 0){
			members = new ArrayList<Member>();
			for(ContributorsType ct : contributorsType){
				Contributors c = (Contributors) em.createQuery("from Contributors where cid=" + ct.getCid() + "").getSingleResult();
				Member m = c.getMemberProfile();
				if(m != null){
					members.add(m);
				}	
			}
		}
		return members;
	}
}
